package com.zonhar.autofeed.cron;

import cn.hutool.extra.mail.MailUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zonhar.autofeed.common.config.AppConfig;
import com.zonhar.autofeed.common.enums.FeedTypeEnum;
import com.zonhar.autofeed.common.device.rpi.RpiDeviceUtils;
import com.zonhar.autofeed.common.exception.RaspberrypiException;
import com.zonhar.autofeed.entities.entity.DistConfigEntity;
import com.zonhar.autofeed.entities.entity.SensorEntity;
import com.zonhar.autofeed.service.AutoFeedWeightService;
import com.zonhar.autofeed.service.DistConfigService;
import com.zonhar.autofeed.service.SensorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 投喂狗检测
 */
@Component
public class FeedDogCron {

    private final static Logger log = LoggerFactory.getLogger(FeedDogCron.class);

    @Autowired
    private AppConfig appConfig;

    @Autowired
    private AutoFeedWeightService autoFeedWeightService;

    @Autowired
    private DistConfigService distConfigService;

    @Autowired
    private SensorService sensorService;

  //  @Scheduled(cron="*/10 * * * * ?")
    public void feedRun() throws RaspberrypiException {
        if(getAutoFeedSwitch()){
            log.info("检测暖暖碗吃完东西没有");
            try {
                if(getHx11Status()){
                    long weight = RpiDeviceUtils.getHx711Weight();
                    log.info("HC700传感器重量：{}",weight);
                    if(weight <= Double.parseDouble(getWarningKg())) {
                        //MusicPlayer.feedPlay();
                        log.warn("碗重量：{},开始下粮",weight);
                        Integer feedCount = getFeedCount();
                        autoFeedWeightService.saveInfo(FeedTypeEnum.AUTO_FEED,weight,feedCount);
                        MailUtil.send(getSendEmail(),"猫粮自动投喂机通知",String.format("自动投放%s份猫粮完毕",feedCount+""),false);
                    }else {
                        log.info("碗重量：{},不需要下粮",weight);
                    }
                }else {
                    log.info("称重传感器已停用");
                }
            }catch (Exception e){
                throw new RaspberrypiException(e.getMessage());
            }

        }
    }

    /**
     * 自动检测猫粮重量
     */
    //@Scheduled(cron="*/10 * * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void testFeed() {
        if(appConfig.isAutoFeed){
            long weight = RpiDeviceUtils.getHx711Weight();
            autoFeedWeightService.saveInfo(FeedTypeEnum.TEST_WEIGHT,weight,0);
        }
    }

    /**
     * 获取收件人
     * @return
     */
    private String getSendEmail(){
        QueryWrapper<DistConfigEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dist_name","email");
        queryWrapper.eq("dist_group","params");
        DistConfigEntity configEntity = distConfigService.getOne(queryWrapper);
        return configEntity.getDistValue();
    }

    /**
     * 获取定时开关
     * @return
     */
    private Boolean getAutoFeedSwitch(){
        QueryWrapper<DistConfigEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dist_name","cron");
        queryWrapper.eq("dist_group","params");
        DistConfigEntity configEntity = distConfigService.getOne(queryWrapper);
        return Boolean.valueOf(configEntity.getDistValue());
    }

    private Boolean getHx11Status(){
        QueryWrapper<SensorEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("id",1);
        SensorEntity sensorEntity = sensorService.getOne(queryWrapper);
        return sensorEntity.getStat() == 0;
    }

    /**
     * 获取预警重量
     * @return
     */
    private String getWarningKg(){
        QueryWrapper<DistConfigEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dist_name","kg");
        queryWrapper.eq("dist_group","params");
        DistConfigEntity configEntity = distConfigService.getOne(queryWrapper);
        return configEntity.getDistValue();
    }

    /**
     * 获取投喂份数
     * @return
     */
    private Integer getFeedCount(){
        QueryWrapper<DistConfigEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dist_name","feedCount");
        queryWrapper.eq("dist_group","params");
        DistConfigEntity configEntity = distConfigService.getOne(queryWrapper);
        return Integer.valueOf(configEntity.getDistValue());
    }

}
